<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="视频ID" prop="awemeId">
        <el-input v-model="formData.awemeId" placeholder="请输入视频ID" />
      </el-form-item>
      <el-form-item label="视频类型" prop="awemeType">
        <el-select v-model="formData.awemeType" placeholder="请选择视频类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="作者ID" prop="authorUserId">
        <el-input v-model="formData.authorUserId" placeholder="请输入作者ID" />
      </el-form-item>
      <el-form-item label="所有者ID" prop="ownerId">
        <el-input v-model="formData.ownerId" placeholder="请输入所有者ID" />
      </el-form-item>
      <el-form-item label="作者sec_uid" prop="secUid">
        <el-input v-model="formData.secUid" placeholder="请输入作者sec_uid" />
      </el-form-item>
      <el-form-item label="作者名称" prop="nickname">
        <el-input v-model="formData.nickname" placeholder="请输入作者名称" />
      </el-form-item>
      <el-form-item label="预览标题" prop="previewTitle">
        <el-input v-model="formData.previewTitle" placeholder="请输入预览标题" />
      </el-form-item>
      <el-form-item label="标题" prop="desc">
        <el-input v-model="formData.desc" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="标签" prop="caption">
        <el-input v-model="formData.caption" placeholder="请输入标签" />
      </el-form-item>
      <el-form-item label="视频创建时间" prop="aCreateTime">
        <el-date-picker
          v-model="formData.aCreateTime"
          type="date"
          value-format="x"
          placeholder="选择视频创建时间"
        />
      </el-form-item>
      <el-form-item label="视频地址" prop="awemeUrl">
        <el-input v-model="formData.awemeUrl" placeholder="请输入视频地址" />
      </el-form-item>
      <el-form-item label="评论数量" prop="commentCount">
        <el-input v-model="formData.commentCount" placeholder="请输入评论数量" />
      </el-form-item>
      <el-form-item label="点赞数量" prop="diggCount">
        <el-input v-model="formData.diggCount" placeholder="请输入点赞数量" />
      </el-form-item>
      <el-form-item label="下载数量" prop="downloadCount">
        <el-input v-model="formData.downloadCount" placeholder="请输入下载数量" />
      </el-form-item>
      <el-form-item label="打赏数量" prop="playCount">
        <el-input v-model="formData.playCount" placeholder="请输入打赏数量" />
      </el-form-item>
      <el-form-item label="分享数量" prop="shareCount">
        <el-input v-model="formData.shareCount" placeholder="请输入分享数量" />
      </el-form-item>
      <el-form-item label="forward数量" prop="forwardCount">
        <el-input v-model="formData.forwardCount" placeholder="请输入forward数量" />
      </el-form-item>
      <el-form-item label="观看数量" prop="liveWatchCount">
        <el-input v-model="formData.liveWatchCount" placeholder="请输入观看数量" />
      </el-form-item>
      <el-form-item label="收藏数量" prop="collectCount">
        <el-input v-model="formData.collectCount" placeholder="请输入收藏数量" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { AwemeApi, AwemeVO } from '@/api/douyin/aweme'

/** 视频 表单 */
defineOptions({ name: 'AwemeForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  awemeId: undefined,
  awemeType: undefined,
  authorUserId: undefined,
  ownerId: undefined,
  secUid: undefined,
  nickname: undefined,
  previewTitle: undefined,
  desc: undefined,
  caption: undefined,
  aCreateTime: undefined,
  awemeUrl: undefined,
  commentCount: undefined,
  diggCount: undefined,
  downloadCount: undefined,
  playCount: undefined,
  shareCount: undefined,
  forwardCount: undefined,
  liveWatchCount: undefined,
  collectCount: undefined
})
const formRules = reactive({
  awemeId: [{ required: true, message: '视频ID不能为空', trigger: 'blur' }],
  awemeType: [{ required: true, message: '视频类型不能为空', trigger: 'change' }],
  authorUserId: [{ required: true, message: '作者ID不能为空', trigger: 'blur' }],
  ownerId: [{ required: true, message: '所有者ID不能为空', trigger: 'blur' }],
  secUid: [{ required: true, message: '作者sec_uid不能为空', trigger: 'blur' }],
  nickname: [{ required: true, message: '作者名称不能为空', trigger: 'blur' }],
  previewTitle: [{ required: true, message: '预览标题不能为空', trigger: 'blur' }],
  desc: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
  caption: [{ required: true, message: '标签不能为空', trigger: 'blur' }],
  aCreateTime: [{ required: true, message: '视频创建时间不能为空', trigger: 'blur' }],
  awemeUrl: [{ required: true, message: '视频地址不能为空', trigger: 'blur' }],
  commentCount: [{ required: true, message: '评论数量不能为空', trigger: 'blur' }],
  diggCount: [{ required: true, message: '点赞数量不能为空', trigger: 'blur' }],
  downloadCount: [{ required: true, message: '下载数量不能为空', trigger: 'blur' }],
  playCount: [{ required: true, message: '打赏数量不能为空', trigger: 'blur' }],
  shareCount: [{ required: true, message: '分享数量不能为空', trigger: 'blur' }],
  forwardCount: [{ required: true, message: 'forward数量不能为空', trigger: 'blur' }],
  liveWatchCount: [{ required: true, message: '观看数量不能为空', trigger: 'blur' }],
  collectCount: [{ required: true, message: '收藏数量不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await AwemeApi.getAweme(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as AwemeVO
    if (formType.value === 'create') {
      await AwemeApi.createAweme(data)
      message.success(t('common.createSuccess'))
    } else {
      await AwemeApi.updateAweme(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    awemeId: undefined,
    awemeType: undefined,
    authorUserId: undefined,
    ownerId: undefined,
    secUid: undefined,
    nickname: undefined,
    previewTitle: undefined,
    desc: undefined,
    caption: undefined,
    aCreateTime: undefined,
    awemeUrl: undefined,
    commentCount: undefined,
    diggCount: undefined,
    downloadCount: undefined,
    playCount: undefined,
    shareCount: undefined,
    forwardCount: undefined,
    liveWatchCount: undefined,
    collectCount: undefined
  }
  formRef.value?.resetFields()
}
</script>
